/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLMapContour.h>
#include <pgl/algotools/FieldRegLatMap.h>
#include <pgl/core/PGLDataVectors.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


PGL_IMPLEMENT_DYNCREATE(CPGLMapContour, CPGLMapZ);

//! Save settings
void CPGLMapContour::SerializeXML(CPGLArchive& ar)
{
	static const TCHAR szTag[] = _T("CPGLMapContour");

	CString data,str;
	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if(ar.IsStoring())
	{
		xml.AddChildElem(szTag);
		xml.AddChildAttrib(_T("Name"),GetName());
		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// Adding base class
			CPGLMapZ::SerializeXML(ar);

			xml.AddChildElemEx(_T("ShowHeightMap"), m_bShowHeightMap);
			
		xml.OutOfElem();
	}
	else
	{
		if  (!xml.FindChildElem(szTag))
				return;

		xml.IntoElem();
			CPGLMapZ::SerializeXML(ar);

			xml.FindGetChildData(_T("ShowHeightMap"), m_bShowHeightMap);

			GenerateContours();
		xml.OutOfElem();
	}
}


#ifdef _DEBUG
void CPGLMapContour::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLMapZ::Dump( dc );

    // now do the stuff for our specific class
	dc << _T("CPGLMapContour ID ") << GetID() << "\n";
}
void CPGLMapContour::AssertValid() const
{
    // call inherited AssertValid first
    CPGLMapZ::AssertValid();
} 
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLMapContour::CPGLMapContour()
: m_bShowHeightMap(true)
{
}

CPGLMapContour::~CPGLMapContour()
{

}

CPGLMapContour& CPGLMapContour::operator = ( const CPGLMapContour& m)
{
	if (this != &m)
	{
		this->CPGLMapZ::operator =(m);

		m_bShowHeightMap = m.m_bShowHeightMap;
		GenerateContours();
	}
	return *this;
}

void CPGLMapContour::GenerateContours()
{
	using namespace contour;

	if (!m_pField || !GetLattice())
		return;

	TLattice<float>::Rect r;
	GetLattice()->GetBoundingBox(r);
	m_pField->SetLimits( Field::SLimits(r.left, r.right, r.bottom, r.top) );

	m_contour.SetField(m_pField);

	m_contour.Generate();
}


/// plotting to EPS file
void CPGLMapContour::PlotGfx(gfxinterface::CGfxInterface& gfx)
{
	using namespace contour;

	CPGLVectorF vX,vY;
	UINT i;
	ListContour::LineStripList::iterator pos;
	ListContour::LineStrip::iterator is;
	ListContour::LineStripList* pStripList;
	ListContour::LineStrip* pStrip;

	// first if it is visible
	if (!IsVisible())
		return;

	// calling base class plot
	if (m_bShowHeightMap)
		CPGLMapZ::PlotGfx(gfx);

	GFX_ADDCOMMENT(gfx,"--- CPGLMapContour ---");	

	for (int iPlane = 0; iPlane < (int)m_contour.GetPlanesSize(); iPlane++)
	{
		pStripList = m_contour.GetLines(iPlane);
		ASSERT(pStripList);

		for (pos=pStripList->begin(); pos != pStripList->end() ; pos++)
		{
			pStrip=(*pos);
			ASSERT(pStrip);
			if (pStrip->empty())
				continue;
			// using info of strip list
			// pStrip contains the succesive index of the points
			vX.resize(pStrip->size());
			vY.resize(pStrip->size());
			for (i=0, is=pStrip->begin();is!=pStrip->end();i++, is++)
			{
				vX[i] = (float)m_contour.GetXi(*is);
				vY[i] = (float)m_contour.GetYi(*is);
			}
			gfx.DrawLineStrip( vX, vY,0,0.2);
		}
	}
}
